﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBase.Daihoctuonglai;
using Dhtl_Repositry.UnitOfWork;
using System.Data;

namespace Dhtl_Repositry.Repository
{
    public class Vanphongdichvu_Repositry : RepositoryBase<Van_phong_dich_vu>
    {

        public Vanphongdichvu_Repositry()
            : base()
        {
        }

        public Vanphongdichvu_Repositry(WorkUnit unit)
            : base(unit)
        {
        }

        public override int CreateOrUpdate(Van_phong_dich_vu entity)
        {
            try
            {
                Context.Entry(entity).State = entity.Id == 0 ?
                                       EntityState.Added :
                                       EntityState.Modified;
                Context.SaveChanges();
                return entity.Id;
            }
            catch { return 0; }
        }

        public override string Delete(int entityId)
        {
            try
            {
                var entity = Context.Van_phong_dich_vu.SingleOrDefault(o => o.Id == entityId);
                Context.Van_phong_dich_vu.Remove(entity);
                Context.SaveChanges();
                return "success";
            }
            catch { return "error"; }
        }

        public override Van_phong_dich_vu GetById(int entityId)
        {
            try
            {
                return Context.Van_phong_dich_vu.SingleOrDefault(p => p.Id == entityId);
            }
            catch { return null; }
        }

        public override List<Van_phong_dich_vu> List()
        {
            try
            {
                return Context.Van_phong_dich_vu.OrderByDescending(p => p.Datecreate).ToList();
            }
            catch { return null; }
        }

        public override List<Van_phong_dich_vu> GetTableListById(int entityId)
        {
            try
            {
                return Context.Van_phong_dich_vu.OrderByDescending(p => p.Datecreate).ToList();
            }
            catch { return null; }
        }
    }
}
